clear
set matsize 5000
cd <directory>
set matsize 5000

cap program drop cgmreg
cap program drop sub_robust
cap program drop unique

global outopts nocon nor2 excel

do "https://sites.google.com/site/judsoncaskey/data/cgmreg.ado"


**********************************************************************************************************************
**Table 5- Columns 1-3 
**********************************************************************************************************************

use LEDprojecwithtamudatanew.dta, clear
*Generate factory by year, factory by month, day of the week FE
xi i.unit*i.year  i.unit*i.month i.dow

*Spline Variables for Temperature
mkspline wbgtmeant1 19 wbgtmeant2=wbgtmeant, displayknots

*Partial out budgeted efficiency, production line FE, factory by year, factory by month, day of the week FE

foreach var of varlist led wbgtmeant1 wbgtmeant2 precipitation{
areg `var'  _I*, cluster(unit) a(rno)
predict `var'res, resid
}


********** Regressions

*Program for getting confidence intervals from output of boottest
cap program drop getci
program define getci
	matrix ci=r(CI)
	scalar lci=ci[1,1]
	scalar uci=ci[1,2]
	
end

foreach var of varlist meant himeant wbgtmeant {

	cap erase original_tab7ledtemp_`var'.xml
	cap erase original_tab7ledtemp_`var'.txt
	}

	foreach var of varlist wbgtmeant {

	reg ledres `var'1res `var'2res precipitationres, cluster(unit)
				
	su led if e(sample)
	local mean=r(mean)
	
	boottest  `var'1res, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'	
	outreg2 using "original_tab7ledtemp_`var'", stats(coef) noaster keep (`var'1res) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci),mean of dep var, `mean')

	boottest  `var'2res, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'	
	outreg2 using "original_tab7ledtemp_`var'", stats(coef) noaster keep (`var'2res) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci),mean of dep var, `mean')

	boottest  precipitationres, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'	
	outreg2 using "original_tab7ledtemp_`var'", stats(coef) noaster keep ( precipitationres) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci),mean of dep var, `mean')
}



*Standard Allowable Minutes and Budgeted Efficiency regressed on LED adoption


use LEDprojecwithtamudatanew.dta, clear

*dropping factories that always had LED or never adopted LED
bys unit: egen mled=mean(led)
drop if mled==0 | mled==1

bys unit: egen mindateled=min(Date) if led==1
bys unit: egen mindateled1=mean(mindateled)

drop mindateled

ren mindateled1 mindateled
g daterelled=Date-mindateled

g leddaterelled=led*daterelled
*Generate factory by year, factory by month, day of the week FE
xi i.unit*i.year i.unit*i.month i.dow

*Partial out budgeted efficiency, production line FE, factory by year, factory by month, day of the week FE

foreach var of varlist mbudgeteff msam led daterelled leddaterelled wbgtmeant precipitation{
areg `var'  _I*, cluster(unit) a(rno)
predict `var'res, resid
}

*Program for getting confidence intervals from output of boottest
cap program drop getci
program define getci
	matrix ci=r(CI)
	scalar lci=ci[1,1]
	scalar uci=ci[1,2]
	
end

foreach var of varlist mbudgeteff msam {

	cap erase original_tab7_`var'.xml
	cap erase original_tab7_`var'.txt
	}

foreach var of varlist mbudgeteff msam {

	reg `var'res leddaterelledres daterelledres ledres wbgtmeantres precipitationres, cluster(unit)
			
	su `var' if e(sample)
	local mean=r(mean)
	boottest leddaterelledres, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'	
	outreg2 using "original_tab7_`var'", stats(coef) noaster keep (leddaterelledres) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci),mean of dep var, `mean')

	boottest daterelledres, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'	
	outreg2 using "original_tab7_`var'", stats(coef) noaster keep (daterelledres) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci),mean of dep var, `mean')

	
	boottest ledres, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'	
	outreg2 using "original_tab7_`var'", stats(coef) noaster keep (ledres) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci),mean of dep var, `mean')

	boottest  wbgtmeantres, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'	
	outreg2 using "original_tab7_`var'", stats(coef) noaster keep (wbgtmeantres) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci),mean of dep var, `mean')

	boottest  precipitationres, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'	
	outreg2 using "original_tab7_`var'", stats(coef) noaster keep (precipitationres) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci),mean of dep var, `mean')

}




**********************************************************************************************************************
**Table 5- Columns 4-6
**********************************************************************************************************************

*Proportion of A grade tailors hired as outcome variable
use "LEDprojecwithtamudatanew.dta", clear
duplicates drop unit, force
keep unit
tempfile leddataunits
save `leddataunits'

*merge LED status into hiring data
import delimited "PERSONAL_DETAILS.txt.txt", delimiter("$") varnames(1) clear
keep if desgination=="TAILOR" | desgination=="TRAINEE TAILOR"
drop if grade=="NA"

merge m:1 unit using `leddataunits'
tab unit if _merge==2
keep if _merge==3
drop _merge

g dayjoin=substr(doj,1,2)
g monthjoin=substr(doj,4,3)

g yearjoin=substr(doj,-2,2)

destring year, replace
replace yearjoin=2000+yearjoin if yearjoin<90
replace yearjoin=1900+yearjoin if substr(doj,-2,1)=="9"

tostring yearjoin, replace
g datetemp= dayjoin+"-"+ monthjoin+"-"+ yearjoin
g datejoin=date(datetemp,"DMY")
format datejoin %td

g year=year(datejoin)
g month=month(datejoin)
drop if year<2010
drop if year==2010 & month<4
drop if year==2013 & month>7
drop if year>2013

g n=1
collapse (sum) n, by(unit datejoin)
tsset unit datejoin
tsfill
replace n=0 if n==.
merge m:1 unit using "leddetails.dta", gen(m1)

g year=year(datejoin)
g month=month(datejoin)
g dow=dow(datejoin)
g monthy=year+(month/12)
g monthyled=yearled+(monthled/12)
g led=monthy>=monthyled

bys unit: egen dateled=min(date) if led==1

bys unit: egen dateled2=mean(dateled)
*This is mising if unit never got LED. Also replace to missing if unit always had LED

drop dateled
ren dateled2 dateled
g daterelled=datejoin-dateled

bys unit: egen mled=mean(led)
replace daterelled=. if mled==1
g leddaterelled=daterelled*led
tempfile tottailors
*keep if daterel>=-365 & daterel<=365
save `tottailors'

import delimited "PERSONAL_DETAILS.txt.txt", delimiter("$") varnames(1) clear
keep if desgination=="TAILOR" | desgination=="TRAINEE TAILOR"
drop if grade=="NA"
g Agrade=strpos(grade,"A") | strpos(grade,"a")
keep if Agrade==1

merge m:1 unit using `leddataunits'
tab unit if _merge==2
*only 22 not present

keep if _merge==3
drop _merge

g dayjoin=substr(doj,1,2)
g monthjoin=substr(doj,4,3)

g yearjoin=substr(doj,-2,2)

destring year, replace
replace yearjoin=2000+yearjoin if yearjoin<90
replace yearjoin=1900+yearjoin if substr(doj,-2,1)=="9"

tostring yearjoin, replace
g datetemp= dayjoin+"-"+ monthjoin+"-"+ yearjoin
g datejoin=date(datetemp,"DMY")
format datejoin %td

g year=year(datejoin)
g month=month(datejoin)
drop if year<2010
drop if year==2010 & month<4
drop if year==2013 & month>5
drop if year>2013

g n=1
collapse (sum) n, by(unit datejoin)
tsset unit datejoin
tsfill
replace n=0 if n==.

ren n An
merge 1:1 unit datejoin using `tottailors'
g propAjoin=An/n

*ren datejoin date
ren An Anojoined
ren n nojoined
tempfile hired
save `hired', replace


import delimited "PERSONAL_DETAILS.txt.txt", delimiter("$") varnames(1) clear
keep unit tkn_no grade
ren tkn_no tokenno
tempfile gradedata
save `gradedata', replace

use "attendance_merged_tamu", clear
merge m:1 unit tokenno using `gradedata'
keep if _merge==3
drop if grade=="NA"
bys unit tokenno: egen lastdate=max(date)

*Keeping if last date was a month before data ends (31jul2013) so that we actually capture attrited people
*31jul2013 is 19570 so 31st may  is 19539

keep if lastdate>=18353
keep if lastdate<= 19509

duplicates drop unit tokenno lastdate, force

g n=1
collapse (sum) n, by(unit lastdate)

xtset unit lastdate
tsfill
replace n=0 if n==.
tempfile totleft
save `totleft', replace

import delimited "PERSONAL_DETAILS.txt.txt", delimiter("$") varnames(1) clear
keep unit tkn_no grade
ren tkn_no tokenno
tempfile gradedata
save `gradedata', replace

use "attendance_merged_tamu", clear
merge m:1 unit tokenno using `gradedata'
keep if _merge==3
drop if grade=="NA"
g Agrade=strpos(grade,"A") | strpos(grade,"a")
keep if Agrade==1
bys unit tokenno: egen lastdate=max(date)

*Keeping if last date was a month before data ends (31jul2013) so that we actually capture attrited people
*31jul2013 is 19570 so 31st may  is 19539

keep if lastdate>=18353
keep if lastdate<= 19509

duplicates drop unit tokenno lastdate, force

g n=1
collapse (sum) n, by(unit lastdate)

xtset unit lastdate
tsfill
replace n=0 if n==.
ren n An
merge 1:1 unit lastdate using `totleft'

g propAleft=An/n

ren lastdate datejoin 

ren An Anleft
ren n totleft

merge 1:1 unit datejoin using `hired',gen(mm)
ren datejoin date
drop mm
merge m:1 unit using "tamu_allunitsmerge", gen(mm)
keep if mm==3
drop mm year month
g year=year(date)
g month=month(date)
g day=day(date)
drop date
merge m:1 longitude latitude year month day using "weather_2010to2013_allweeklags.dta ", gen(mmm)
keep if mmm==3
drop mmm

g netn=Anojoined- Anleft
su netn, d

cap drop netpropA
g netpropA= propAjoin- propAleft

drop if dow==0

cap drop mled
bys unit: egen mled=mean(led)
drop if mled==0 | mled==1

xi i.unit*i.year i.unit*i.month i.dow

global depvars propAjoin propAleft netpropA 

foreach var of varlist wbgtmeant precipitation $depvars led daterelled leddaterelled{

reg `var' _I*
predict `var'res, res
}

set matsize 5000

*Program for getting confidence intervals from output of boottest
cap program drop getci
program define getci
	matrix ci=r(CI)
	scalar lci=ci[1,1]
	scalar uci=ci[1,2]
	
	end

foreach i of varlist $depvars{

foreach var of varlist led {

cap erase "daterel`var'_timevars_`i'.xml"
cap erase "daterel`var'_timevars_`i'.txt"


}
}
	
foreach i of varlist $depvars{

foreach var of varlist led {

	reg `i'res  leddaterel`var'res daterel`var'res `var'res wbgtmeantres precipitationres, cluster(unit) 
	su `i' if e(sample)
	local mean=r(mean)
	boottest  leddaterel`var'res, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "daterel`var'_timevars_`i'", stats(coef) noaster ctitle("`i'") keep ( leddaterel`var'res) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci),mean of dep var, `mean')

	boottest daterel`var'res, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "daterel`var'_timevars_`i'", stats(coef) noaster ctitle("`i'")  keep (daterel`var'res) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))

	boottest ledres, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "daterel`var'_timevars_`i'", stats(coef) noaster ctitle("`i'")  keep (ledres) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))
	
	boottest wbgtmeantres, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "daterel`var'_timevars_`i'", stats(coef) noaster ctitle("`i'")  keep (wbgtmeantres) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))

	boottest precipitationres, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "daterel`var'_timevars_`i'", stats(coef) noaster ctitle("`i'")  keep (precipitationres) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))
		
	*cap erase  tab3_`var'.txt
}
}



su propAjoin if daterelled!=.

*Proportion of B grade tailors hired as outcome variable

use "LEDprojecwithtamudatanew.dta", clear
duplicates drop unit, force
keep unit
coun
tempfile leddataunits
save `leddataunits'


import delimited "PERSONAL_DETAILS.txt.txt", delimiter("$") varnames(1) clear
keep if desgination=="TAILOR" | desgination=="TRAINEE TAILOR"
drop if grade=="NA"

*99.8% of remaining obs have A, B, or C grade. Dropping the rest.
*eep if strpos(grade,"A") | strpos(grade,"a") | strpos(grade,"B") | strpos(grade,"b") | strpos(grade,"C") | strpos(grade,"c")

merge m:1 unit using `leddataunits'
tab unit if _merge==2
*only 22 not present

keep if _merge==3
drop _merge

g dayjoin=substr(doj,1,2)
g monthjoin=substr(doj,4,3)

g yearjoin=substr(doj,-2,2)

destring year, replace
replace yearjoin=2000+yearjoin if yearjoin<90
replace yearjoin=1900+yearjoin if substr(doj,-2,1)=="9"

tostring yearjoin, replace
g datetemp= dayjoin+"-"+ monthjoin+"-"+ yearjoin
g datejoin=date(datetemp,"DMY")
format datejoin %td

g year=year(datejoin)
g month=month(datejoin)
drop if year<2010
drop if year==2010 & month<4
drop if year==2013 & month>7
drop if year>2013

g n=1
collapse (sum) n, by(unit datejoin)
tsset unit datejoin
tsfill
replace n=0 if n==.
merge m:1 unit using "leddetails.dta", gen(m1)


g year=year(datejoin)
g month=month(datejoin)
g dow=dow(datejoin)
g monthy=year+(month/12)
g monthyled=yearled+(monthled/12)
g led=monthy>=monthyled

bys unit: egen dateled=min(date) if led==1

bys unit: egen dateled2=mean(dateled)
*This is mising if unit never got LED. Also replace to missing if unit always had LED

drop dateled
ren dateled2 dateled
g daterelled=datejoin-dateled

bys unit: egen mled=mean(led)
replace daterelled=. if mled==1
g leddaterelled=daterelled*led
tempfile tottailors
*keep if daterel>=-365 & daterel<=365
save `tottailors'


import delimited "PERSONAL_DETAILS.txt.txt", delimiter("$") varnames(1) clear
keep if desgination=="TAILOR" | desgination=="TRAINEE TAILOR"
drop if grade=="NA"
g Bgrade=strpos(grade,"B") | strpos(grade,"b")
keep if Bgrade==1

merge m:1 unit using `leddataunits'
tab unit if _merge==2
*only 22 not present

keep if _merge==3
drop _merge

g dayjoin=substr(doj,1,2)
g monthjoin=substr(doj,4,3)

g yearjoin=substr(doj,-2,2)

destring year, replace
replace yearjoin=2000+yearjoin if yearjoin<90
replace yearjoin=1900+yearjoin if substr(doj,-2,1)=="9"

tostring yearjoin, replace
g datetemp= dayjoin+"-"+ monthjoin+"-"+ yearjoin
g datejoin=date(datetemp,"DMY")
format datejoin %td

g year=year(datejoin)
g month=month(datejoin)
drop if year<2010
drop if year==2010 & month<4
drop if year==2013 & month>5
drop if year>2013

g n=1
collapse (sum) n, by(unit datejoin)
tsset unit datejoin
tsfill
replace n=0 if n==.

ren n Bn
merge 1:1 unit datejoin using `tottailors'
g propBjoin=Bn/n

*ren datejoin date
ren Bn Bnojoined
ren n nojoined
tempfile hired
save `hired', replace


import delimited "PERSONAL_DETAILS.txt.txt", delimiter("$") varnames(1) clear
keep unit tkn_no grade
ren tkn_no tokenno
tempfile gradedata
save `gradedata', replace

use "attendance_merged_tamu", clear
merge m:1 unit tokenno using `gradedata'
keep if _merge==3
drop if grade=="NA"
bys unit tokenno: egen lastdate=max(date)

*Keeping if last date was a month before data ends (31jul2013) so that we actually capture attrited people
*31jul2013 is 19570 so 31st may  is 19539

keep if lastdate>=18353
keep if lastdate<= 19509

duplicates drop unit tokenno lastdate, force

g n=1
collapse (sum) n, by(unit lastdate)

xtset unit lastdate
tsfill
replace n=0 if n==.
tempfile totleft
save `totleft', replace

import delimited "PERSONAL_DETAILS.txt.txt", delimiter("$") varnames(1) clear
keep unit tkn_no grade
ren tkn_no tokenno
tempfile gradedata
save `gradedata', replace

use "attendance_merged_tamu", clear
merge m:1 unit tokenno using `gradedata'
keep if _merge==3
drop if grade=="NA"
g Bgrade=strpos(grade,"B") | strpos(grade,"b")
keep if Bgrade==1
bys unit tokenno: egen lastdate=max(date)

*Keeping if last date was a month before data ends (31jul2013) so that we actually capture attrited people
*31jul2013 is 19570 so 31st may  is 19539

keep if lastdate>=18353
keep if lastdate<= 19509

duplicates drop unit tokenno lastdate, force

g n=1
collapse (sum) n, by(unit lastdate)

xtset unit lastdate
tsfill
replace n=0 if n==.
ren n Bn
merge 1:1 unit lastdate using `totleft'

g propBleft=Bn/n

ren lastdate datejoin 

ren Bn Bnleft
ren n totleft

merge 1:1 unit datejoin using `hired',gen(mm)
ren datejoin date
drop mm
merge m:1 unit using "tamu_allunitsmerge", gen(mm)
keep if mm==3
drop mm year month
g year=year(date)
g month=month(date)
g day=day(date)
drop date
merge m:1 longitude latitude year month day using "weather_2010to2013_allweeklags.dta ", gen(mmm)
keep if mmm==3
drop mmm



g netn= Bnojoined- Bnleft
su netn, d

cap drop netpropB
g netpropB= propBjoin- propBleft

drop if dow==0

cap drop mled
bys unit: egen mled=mean(led)
drop if mled==0 | mled==1

xi i.unit*i.year i.unit*i.month i.dow

global depvars propBjoin propBleft netpropB

foreach var of varlist wbgtmeant precipitation $depvars led daterelled leddaterelled{

reg `var' _I*
predict `var'res, res
}

set matsize 5000

*Program for getting confidence intervals from output of boottest
cap program drop getci
program define getci
	matrix ci=r(CI)
	scalar lci=ci[1,1]
	scalar uci=ci[1,2]
	
	end

foreach i of varlist $depvars{

foreach var of varlist led {

cap erase "daterel`var'_timevars_`i'.xml"
cap erase "daterel`var'_timevars_`i'.txt"



}
}
	
foreach i of varlist $depvars{

foreach var of varlist led {

	reg `i'res  leddaterel`var'res daterel`var'res `var'res wbgtmeantres precipitationres, cluster(unit) 
	su `i' if e(sample)
	local mean=r(mean)
	
	boottest  leddaterel`var'res, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "daterel`var'_timevars_`i'", stats(coef) noaster ctitle("`i'") keep ( leddaterel`var'res) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci),mean of dep var, `mean')

	boottest daterel`var'res, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "daterel`var'_timevars_`i'", stats(coef) noaster ctitle("`i'")  keep (daterel`var'res) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))

	boottest ledres, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "daterel`var'_timevars_`i'", stats(coef) noaster ctitle("`i'")  keep (ledres) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))
	
	boottest wbgtmeantres, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "daterel`var'_timevars_`i'", stats(coef) noaster ctitle("`i'")  keep (wbgtmeantres) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))

	boottest precipitationres, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "daterel`var'_timevars_`i'", stats(coef) noaster ctitle("`i'")  keep (precipitationres) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))
		
	*cap erase  tab3_`var'.txt
}
}


su propBjoin if daterelled!=.


*Proportion of B grade tailors hired as outcome variable

use "LEDprojecwithtamudatanew.dta", clear
duplicates drop unit, force
keep unit
coun
tempfile leddataunits
save `leddataunits'


import delimited "PERSONAL_DETAILS.txt.txt", delimiter("$") varnames(1) clear
keep if desgination=="TAILOR" | desgination=="TRAINEE TAILOR"
drop if grade=="NA"

*99.8% of remaining obs have A, B, or C grade. Dropping the rest.
*eep if strpos(grade,"A") | strpos(grade,"a") | strpos(grade,"B") | strpos(grade,"b") | strpos(grade,"C") | strpos(grade,"c")

merge m:1 unit using `leddataunits'
tab unit if _merge==2
*only 22 not present

keep if _merge==3
drop _merge

g dayjoin=substr(doj,1,2)
g monthjoin=substr(doj,4,3)

g yearjoin=substr(doj,-2,2)

destring year, replace
replace yearjoin=2000+yearjoin if yearjoin<90
replace yearjoin=1900+yearjoin if substr(doj,-2,1)=="9"

tostring yearjoin, replace
g datetemp= dayjoin+"-"+ monthjoin+"-"+ yearjoin
g datejoin=date(datetemp,"DMY")
format datejoin %td

g year=year(datejoin)
g month=month(datejoin)
drop if year<2010
drop if year==2010 & month<4
drop if year==2013 & month>7
drop if year>2013

g n=1
collapse (sum) n, by(unit datejoin)
tsset unit datejoin
tsfill
replace n=0 if n==.
merge m:1 unit using "leddetails.dta", gen(m1)


g year=year(datejoin)
g month=month(datejoin)
g dow=dow(datejoin)
g monthy=year+(month/12)
g monthyled=yearled+(monthled/12)
g led=monthy>=monthyled

bys unit: egen dateled=min(date) if led==1

bys unit: egen dateled2=mean(dateled)
*This is mising if unit never got LED. Also replace to missing if unit always had LED

drop dateled
ren dateled2 dateled
g daterelled=datejoin-dateled

bys unit: egen mled=mean(led)
replace daterelled=. if mled==1
g leddaterelled=daterelled*led
tempfile tottailors
*keep if daterel>=-365 & daterel<=365
save `tottailors'


import delimited "PERSONAL_DETAILS.txt.txt", delimiter("$") varnames(1) clear
keep if desgination=="TAILOR" | desgination=="TRAINEE TAILOR"
drop if grade=="NA"
g Cgrade=strpos(grade,"C") | strpos(grade,"c")
keep if Cgrade==1

merge m:1 unit using `leddataunits'
tab unit if _merge==2
*only 22 not present

keep if _merge==3
drop _merge

g dayjoin=substr(doj,1,2)
g monthjoin=substr(doj,4,3)

g yearjoin=substr(doj,-2,2)

destring year, replace
replace yearjoin=2000+yearjoin if yearjoin<90
replace yearjoin=1900+yearjoin if substr(doj,-2,1)=="9"

tostring yearjoin, replace
g datetemp= dayjoin+"-"+ monthjoin+"-"+ yearjoin
g datejoin=date(datetemp,"DMY")
format datejoin %td

g year=year(datejoin)
g month=month(datejoin)
drop if year<2010
drop if year==2010 & month<4
drop if year==2013 & month>5
drop if year>2013

g n=1
collapse (sum) n, by(unit datejoin)
tsset unit datejoin
tsfill
replace n=0 if n==.

ren n Cn
merge 1:1 unit datejoin using `tottailors'
g propCjoin=Cn/n

*ren datejoin date
ren Cn Cnojoined
ren n nojoined
tempfile hired
save `hired', replace


import delimited "PERSONAL_DETAILS.txt.txt", delimiter("$") varnames(1) clear
keep unit tkn_no grade
ren tkn_no tokenno
tempfile gradedata
save `gradedata', replace

use "attendance_merged_tamu", clear
merge m:1 unit tokenno using `gradedata'
keep if _merge==3
drop if grade=="NA"
bys unit tokenno: egen lastdate=max(date)

*Keeping if last date was a month before data ends (31jul2013) so that we actually capture attrited people
*31jul2013 is 19570 so 31st may  is 19539

keep if lastdate>=18353
keep if lastdate<= 19509

duplicates drop unit tokenno lastdate, force

g n=1
collapse (sum) n, by(unit lastdate)

xtset unit lastdate
tsfill
replace n=0 if n==.
tempfile totleft
save `totleft', replace

import delimited "PERSONAL_DETAILS.txt.txt", delimiter("$") varnames(1) clear
keep unit tkn_no grade
ren tkn_no tokenno
tempfile gradedata
save `gradedata', replace

use "attendance_merged_tamu", clear
merge m:1 unit tokenno using `gradedata'
keep if _merge==3
drop if grade=="NA"
g Cgrade=strpos(grade,"C") | strpos(grade,"c")
keep if Cgrade==1
bys unit tokenno: egen lastdate=max(date)

*Keeping if last date was a month before data ends (31jul2013) so that we actually capture attrited people
*31jul2013 is 19570 so 31st may  is 19539

keep if lastdate>=18353
keep if lastdate<= 19509

duplicates drop unit tokenno lastdate, force

g n=1
collapse (sum) n, by(unit lastdate)

xtset unit lastdate
tsfill
replace n=0 if n==.
ren n Cn
merge 1:1 unit lastdate using `totleft'

g propCleft=Cn/n

ren lastdate datejoin 

ren Cn Cnleft
ren n totleft

merge 1:1 unit datejoin using `hired',gen(mm)
ren datejoin date
drop mm
merge m:1 unit using "tamu_allunitsmerge", gen(mm)
keep if mm==3
drop mm year month
g year=year(date)
g month=month(date)
g day=day(date)
drop date
merge m:1 longitude latitude year month day using "weather_2010to2013_allweeklags.dta ", gen(mmm)
keep if mmm==3
drop mmm
g netn=Cnojoined- Cnleft
su netn, d

cap drop netpropC
g netpropC= propCjoin- propCleft

drop if dow==0
cap drop mled
bys unit: egen mled=mean(led)
drop if mled==0 | mled==1

xi i.unit*i.year i.unit*i.month i.dow

global depvars propCjoin propCleft netpropC

foreach var of varlist wbgtmeant precipitation $depvars led daterelled leddaterelled{

reg `var' _I*
predict `var'res, res
}

set matsize 5000

*Program for getting confidence intervals from output of boottest
cap program drop getci
program define getci
	matrix ci=r(CI)
	scalar lci=ci[1,1]
	scalar uci=ci[1,2]
	
	end
	

foreach i of varlist $depvars{

foreach var of varlist led {

cap erase "daterel`var'_timevars_`i'.xml"
cap erase "daterel`var'_timevars_`i'.txt"


}
}
	
foreach i of varlist $depvars{

foreach var of varlist led {

	reg `i'res  leddaterel`var'res daterel`var'res `var'res wbgtmeantres precipitationres, cluster(unit) 
	su `i' if e(sample)
	local mean=r(mean)
	
	boottest  leddaterel`var'res, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "daterel`var'_timevars_`i'", stats(coef) noaster ctitle("`i'") keep ( leddaterel`var'res) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci),mean of dep var, `mean')

	boottest daterel`var'res, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "daterel`var'_timevars_`i'", stats(coef) noaster ctitle("`i'")  keep (daterel`var'res) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))

	boottest ledres, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "daterel`var'_timevars_`i'", stats(coef) noaster ctitle("`i'")  keep (ledres) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))
	
	boottest wbgtmeantres, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "daterel`var'_timevars_`i'", stats(coef) noaster ctitle("`i'")  keep (wbgtmeantres) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))

	boottest precipitationres, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "daterel`var'_timevars_`i'", stats(coef) noaster ctitle("`i'")  keep (precipitationres) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))
		
}
}


su propCjoin if daterelled!=.
